// Domino.cpp: implementation of the CDomino class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Domino.h"
#include <log.h>

#include "Configuration.h"

int _getLookupInfo (FilterContext* context,
					char *pMatch,
					int  itemNumber,
					char **pInfo,
                   int  *pInfoLen);
bool _getNextItem(FilterContext* context, char* pNamespace, char*& pItem, int& cbItem);

int _getUserNames (FilterContext* context, char *userName, 
                  char **ppUserShortName,  int  *pUserShortNameLen,
                  char **ppF_PWDCardNum,  int  *pF_PWDCardNumLen)
{
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): _getUserNames(): call"), NULLHANDLE, NOERROR, FILTER_NAME );
	int error = NOERROR;
	HANDLE hLookup = NULL;
	WORD usMatches = 0;
	char *pLookup;
	char *pNamespace = NULL;
	char *pMatch = NULL;
	int     rc = -1;

	if (!userName ||
		!ppUserShortName || !pUserShortNameLen || !ppF_PWDCardNum || !pF_PWDCardNumLen)
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getUserNames(): (!userName ||...)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return rc;
	}
	
 /* Initialize output */
	*ppUserShortName = NULL;
	*pUserShortNameLen = 0;

 /* do the name lookup
  */
	 error = NAMELookup(NULL, /* NULL means look locally */
							   0,   /* flags */
							   1,   /* number of namespaces */
							   "$Users", /* namespace list */
							   1,   /* number of names to lookup */
							   userName, /* list of names to lookup */
							   2, /* number of items to return */
							   "ShortName\0F_PWDCardNum", /* list of items to
													   * return */
							   &hLookup); /* place to receive handle of
										   * return buffer */
      
	 if (error || (NULLHANDLE == hLookup))
	  goto NoUnlockExit;

	 pLookup = (char *) OSLockObject(hLookup);
  
	 /* Get a pointer to our entry.
	  */
	 pNamespace = (char *)NAMELocateNextName(pLookup, /* name lookup
														 * buffer */
											NULL, /* start at beginning of
												   * lookup buffer */
		 &usMatches); /* Receives number
														* of times we
														* found the entry
														* (should be 1)
														*/

	 /* If we didn't find the entry, then quit */
	 if ((pNamespace == NULL) || (usMatches <= 0)) {
		 if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getUserNames(): ((pName == NULL) || (Matches <= 0))"), NULLHANDLE, NOERROR, FILTER_NAME );
	  goto Exit;
	 }
	 pMatch = (char *)NAMELocateNextMatch(pLookup,  /* name lookup
															* buffer */
										  pNamespace, /* entry that we found */
										  NULL); /* no previous match */

	if (NULL == pMatch)
	{
		goto Exit;
	}
// Get authentication username
	 if ( _getLookupInfo (context,
								pMatch,
								0,
								ppUserShortName,
								pUserShortNameLen) )
	  goto Exit;
	 else
	 {
		 if (CConfiguration::IsDebugModule())
			 LogEventText( _T("%s(Debug): _getUserNames(): ppUserShortName='%s'"), NULLHANDLE, NOERROR, FILTER_NAME, *ppUserShortName );
	 }
// Get F_PWDCardNum
	 if( _getLookupInfo (context,
								pMatch,
								1,
								ppF_PWDCardNum,
								pF_PWDCardNumLen) )
								goto Exit;
	 else
	 {
		 if (CConfiguration::IsDebugModule())
			 LogEventText( _T("%s(Debug): _getUserNames(): ppF_PWDCardNum='%s'"), NULLHANDLE, NOERROR, FILTER_NAME, *ppF_PWDCardNum );
	  /* Success in all things */
	  rc = 0;
	 }

Exit:
	if ( pLookup && hLookup )
		OSUnlock(hLookup);

NoUnlockExit:
	if (NULLHANDLE != hLookup)
		OSMemFree(hLookup);
	return rc;
}

int _getLookupInfo (FilterContext* context,
                   char *pMatch,
                   int  itemNumber,
                   char **pInfo,
                   int  *pInfoLen)
{
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): _getLookupInfo(): call"), NULLHANDLE, NOERROR, FILTER_NAME );

	if (NULL == context)
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getLookupInfo(): (NULL == context)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return( -1 );
	}

	unsigned int reserved = 0;
	unsigned int errID;
	char *ValuePtr = NULL;
	WORD ValueLength, DataType;
	STATUS error;
	void *newSpace = NULL;

	if (!pMatch || !pInfo || !pInfoLen || (itemNumber < 0))
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getLookupInfo(): (!pMatch || !pInfo || !pInfoLen || (itemNumber < 0)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return -1;
	}

	/* Initialize output */
	*pInfo = NULL;
	*pInfoLen = 0;
	
	/* Check the type and length of the info */
	ValuePtr = (char *)NAMELocateItem(pMatch, /* match that we
												   * found */
									(WORD)itemNumber, /* item # in order
												 *of item on lookup */
									&DataType, /* return the datatype
												* of item value */
									&ValueLength); /* size of rtn
	* value */
	if (NULL == ValuePtr || ValueLength == 0)
	{
		/* there is no info */
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getLookupInfo(): (NULL == ValuePtr || ValueLength == 0)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return -1;
	}
	ValueLength -= sizeof(WORD); /* remove datatype word included 
									  * in the list length 
									  */
	/* check the value DataType */
	switch (DataType)
	{
		case TYPE_TEXT_LIST:
		break;

		case TYPE_TEXT:
		break;

		default:
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): _getLookupInfo(): switch (DataType=default)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return -1;
	}

	newSpace = (context->AllocMem)(context, ValueLength+1,
									   reserved, &errID);
	*pInfo = (char *) newSpace;
	if (NULL == *pInfo)
	{
		printf ("Out of memory\n");
		return -1;
	}

	/* Get the info */
	error = NAMEGetTextItem(pMatch,     /* match that we found */
						 (WORD)itemNumber, /* item # in order of item
											 * on lookup */
						 (WORD)0, /* Member # of item in text
									* lists */
						 *pInfo, /* buffer to copy result
										 * into */
						 (WORD)(ValueLength+1)); /* Length of buffer */
	if (!error)
	{
		*pInfoLen = ValueLength +1;
		return 0;
	}
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): _getLookupInfo(): return -1"), NULLHANDLE, NOERROR, FILTER_NAME );
	return -1;
}

//////////////////////////////////////////////////////////////////////
// Interface
//////////////////////////////////////////////////////////////////////

bool CDomino::GetAuthName(char* pszUsername, char*& pszAuthName, int& cbAuthName)
{
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): CDomino::GetAuthName(): call"), NULLHANDLE, NOERROR, FILTER_NAME );

	if (NULL == m_pContext)
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): CDomino::GetAuthName(): (NULL == m_pContext)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return( false );
	}

	if (NULL == pszUsername)
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): CDomino::GetAuthName(): (NULL == pszUsername)"), NULLHANDLE, NOERROR, FILTER_NAME );
		return( false );
	}

	char* pszF_PWDCardNum= NULL;
	int cbF_PWDCardNum= 0;
	if (0 != _getUserNames( m_pContext, pszUsername, &pszAuthName, &cbAuthName, &pszF_PWDCardNum, &cbF_PWDCardNum ))
		return( false );

	if ((0 == cbF_PWDCardNum)
	 || ('\0' == *pszF_PWDCardNum))
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): CDomino::GetAuthName(): F_PWDCardNum is NULL"), NULLHANDLE, NOERROR, FILTER_NAME );
		return( false );
	}
	else
	{
		if (CConfiguration::IsDebugModule())
			LogEventText( _T("%s(Debug): CDomino::GetAuthName(): F_PWDCardNum='%s'"), NULLHANDLE, NOERROR, FILTER_NAME, pszF_PWDCardNum );
	}
	
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): CDomino::GetAuthName(): AuthName='%s'"), NULLHANDLE, NOERROR, FILTER_NAME, pszAuthName );
	return( true );
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDomino::CDomino(FilterContext* pContext)
	: m_pContext( pContext )
{
	if (CConfiguration::IsDebugModule())
		LogEventText( _T("%s(Debug): CDomino::CDomino(): call"), NULLHANDLE, NOERROR, FILTER_NAME );
}

CDomino::~CDomino()
{

}
